package utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 信贷业务申请到期时间工具类
 *
 * @author wujingjie
 * @version V2.1
 * @since 2020/8/27 19:04
 */
public class EndDateUtils {

    /** 最大 时 */
    private static final int MAX_HOUR = 23;
    /** 最大 分 */
    private static final int MAX_MINUTE = 59;
    /** 最大 秒 */
    private static final int MAX_SECOND = 59;

    /**
     * 获取信贷业务申请到期时间
     * @param startDate 申请时间
     * @param termUnit  期限单位
     * @param term  期限
     * @return
     */
    public static Date getEndDate(Date startDate,EnumTermUnit termUnit,int term){
        if(startDate == null || termUnit.getCode() == null){
            return new Date();
        }

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        calendar.set(Calendar.HOUR_OF_DAY, MAX_HOUR);
        calendar.set(Calendar.MINUTE, MAX_MINUTE);
        calendar.set(Calendar.SECOND, MAX_SECOND);
        calendar.set(Calendar.MILLISECOND,0);
        switch (termUnit){
            case YEAR:
                calendar.add(Calendar.YEAR, term);
                break;
            case MONTH:
                calendar.add(Calendar.MONTH, term);
                break;
            case DAY:
                calendar.add(Calendar.DATE, term);
                break;
            default:
                return calendar.getTime();
        }
        return calendar.getTime();
    }
    public static List<String> getRecentMonths(Integer amount) {
        List<String> months = new ArrayList<>();
        for (int i = amount; i > 0 ; i--) {
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH,-i);
            Date time = c.getTime();
            String month = DateUtil.date2String(time, "yyyy-MM");
            months.add(month);
        }
        return months;
    }

    public static void main(String[] args) {
        List<String> recentMonths = getRecentMonths(6);
        System.out.println(recentMonths);

    }
}
